<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../easyui/1.3.4/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="../easyui/1.3.4/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/1.3.4/locale/easyui-lang-zh_CN.js"></script>
<script src="../js/jquery.serializejson.min.js"></script>
</head>
<body>
<div id="users-toolbar">
        <div class="wu-toolbar-button">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openUserAdd()" plain="true">添加评论</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openUserEdit()" plain="true">修改评论</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeUser()" plain="true">删除评论</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reloadUser()" plain="true">刷新评论</a>
        </div>
        <div class="wu-toolbar-search">
            <label>起始时间：</label><input class="easyui-datebox" style="width:100px">
            <label>结束时间：</label><input class="easyui-datebox" style="width:100px">
            <label>用户组：</label> 
            <select class="easyui-combobox" panelHeight="auto" style="width:100px">
                <option value="0">选择用户组</option>
                <option value="1">黄钻</option>
                <option value="2">红钻</option>
                <option value="3">蓝钻</option>
            </select>
            <label>关键词：</label><input class="wu-text" style="width:100px">
            <a href="#" class="easyui-linkbutton" iconCls="icon-search">开始检索</a>
        </div>
</div>
<table id="users-datagrid"></table>
<div id="users-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="users-form" method="post">
        <table>
            <tr>
                <td width="60" align="right">评论者:</td>
                <td><input type="text" name="author" class="wu-text wu-text0" /></td>
            </tr>
            <tr>
                <td align="right">内 容:</td>
                <td><input type="text" name="content" class="wu-text wu-text1" /></td>
            </tr>
          
        </table>
   </form>
</div>
<script>
	//跳转
	function GetQueryString(name)
	{
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	}
//	alert(GetQueryString("id"))
	
	$.ajax({
            type:"post",
            url:"http://localhost:3000/say/all",
            success:function(res){
                for(var i in res){
                    if(GetQueryString("id")==res[i].cateId){
                     initList(res[i].cateId)
                    }
                }
            }
        })
	function initList(cateId){
			var postUrl;
			if(cateId !=undefined && cateId.trim().length>0){
				queryParams = {cateId: cateId}
			}else{
				queryParams = {};
			}					
			$("#users-datagrid").datagrid({
				url:'http://localhost:3000/say/list',
				method:'post',
				pagination:true,
				queryParams: queryParams,
				fit:true,
				columns:[[
						{ field:'author',title:'评论者',width:100,sortable:true},
						{ field:'content',title:'评论内容',width:100,sortable:true},
						{ field:'data',title:'评论时间',width:100,sortable:true},
						{field:'Games',title:'管理',width:50,
						        formatter: function(value,row,index){
									return '<a href="javascript:void(0)" style="display:inline-block;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);padding:3px 10px;border-radius: 5px 5px 5px 5px" onclick="editUser(\''+ row._id+'\')">修改</a> '+
									'<a href="javascript:void(0)" style="display:inline-block;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);padding:3px 10px;border-radius: 5px 5px 5px 5px" onclick="removeUser(\''+ row._id+'\')">删除</a>'
					
									}}	
					]]	    
			})		
		}	
	
function openUserAdd(){
		$("#users-form").form("clear");
		$('#users-dialog').dialog({
			closed: false,
			modal:true,
            title: "添加信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userAddAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#users-dialog').dialog('close');                    
                }
            }]
        });
}
////添加	
function userAddAction(){
	$('#users-form').form('submit',{
        onSubmit:function(){
        	console.log($("#users-form").serialize)
            if($(this).form('enableValidation').form('validate')){
            	$.ajax({
            		type:"post",
            		url:"http://localhost:3000/news/data",
            		data:$("#users-form").serializeJSON()
            	}).then(function(res){
            		console.log(res)
            	})
            }else{
            	alert("no")
            }
        }
    });
    $('#users-dialog').dialog("close");
    $('#users-datagrid').datagrid("reload")
//	console.log("form post")
//	console.log($("#user-form").serializeArray())
}
function reloadUser(){
	$('#users-datagrid').datagrid("reload")
}
//////删除
function removeUser(){
	var node = $('#users-datagrid').datagrid('getSelected');
	var id=node._id;
	$.ajax({
		type:"delete",
		url:"http://localhost:3000/news/data/"+id
	}).then(function(res){
		$('#users-datagrid').datagrid("reload")
	})
}
$("#users-datagrid").datagrid({
		url:"http://localhost:3000/news/list",
		fit:true,
		method:"post",	
		rownumbers:true,
		singleSelect:false,        
		pagination:true,
		multiSort:true,
		fitColumns:true,
		toolbar:"#user-toolbar",
		columns:[[
			{ field:'author',title:'评论者',width:100,sortable:true},
			{ field:'content',title:'评论内容',width:100,sortable:true},
			{ field:'data',title:'评论时间',width:100,sortable:true},
			{field:'Games',title:'管理',width:50,
			        formatter: function(value,row,index){
						return '<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="editUser(\''+ row._id+'\')">修改</a> '+
						'<a href="javascript:void(0)"style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="removeUser(\''+ row._id+'\')">删除</a>'
		
						}}	
		]]
	})
////修改
function openUserEdit(){
	$("#users-form").form("clear");
		$('#users-dialog').dialog({			
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userEditAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#users-dialog').dialog('close');                    
                }
            }]
        });
        var node = $('#users-datagrid').datagrid('getSelected');
			console.log(node)
		var id=node._id;
        $.ajax({
			type:"post",   
			url:"http://localhost:3000/news/data/"+id
		}).then(function(res){
			$(".wu-text0").val(node.author);
			$(".wu-text1").val(node.content);		
		})
}
function userEditAction(id){
	var node = $('#users-datagrid').datagrid('getSelected');
	var id=node._id;
	var postUrl;
	if($(".wu-text").val()){
		$(".wu-text0").val(node.author);
		$(".wu-text1").val(node.content);
	}else{
		$(".wu-text0").val();
		$(".wu-text1").val();
	}
	var formData = $('#users-form').serializeJSON();
	console.log(formData)
	$.ajax({
		type:"post",
		url:"http://localhost:3000/news/data/"+id,
		data: formData
	}).then(function(res){
		$('#users-dialog').dialog("close");
		$('#users-datagrid').datagrid("reload")
	})
}
////修改
function editUser(cateId){
	console.log(cateId)
	queryParams ={cateId:cateId}
	console.log(queryParams)

	$('#users-dialog').dialog({			
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userEditAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#users-dialog').dialog('close');                    
                }
            }]
        });	
}




</script>
</body>
</html>